package com.lvan.websocket.chat.config;

import javax.websocket.Session;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 存放Session会话连接池
 *
 * @author Lvan
 */
public class SessionPool {

    /**
     * 存放Session集合
     * ConcurrentHashMap是线程安全的。
     */
    public static Map<String, Session> sessions = new ConcurrentHashMap<>();

    /**
     * 移除Session连接池中每一会话
     *
     * @param sessionId SessionID
     */
    public static void close(String sessionId) {
        for (String user : SessionPool.sessions.keySet()) {
            Session session = SessionPool.sessions.get(user);
            if (session.getId().equals(sessionId)) {
                sessions.remove(user);
                break;
            }
        }
    }
}
